// pages/login/login.js
import http from '../../http/http.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    topheight: getApp().globalData.topheight,
    phone: '',
    code: '',
    codeget:true,
    times:60,
    ac_phone:false,
    ac_code:false

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    
  },
  onShow: function() {
    let _this = this
    wx.getSystemInfo({
      success: function(res) {
        _this.data.topheight = res.statusBarHeight;
      }
    })
  },
  // 登录跳转
  submit: function() {
    let data={
      mobile:this.data.phone,
      captcha:this.data.code
    }
    http.postdata('/sys/phone/login',data).then((res) => {
      wx.setStorageSync('token',res.data.result.token)
      wx.redirectTo({
        url: '../home/home',
      })
      console.log(res)
      
    })
  },
  //获取手机号码、
  getValue: function(e) {
    console.log(e)
    let val = e.detail.value
    let type = e.currentTarget.dataset.type
    if(type=='phone'){
      this.data.phone = val
    }else{
      this.data.code = val
    }
    
  },
  // 获取值
  active:function(e){
    let val = e.detail.value
    let type = e.currentTarget.dataset.type
    if (type == 'phone') {
      this.setData({
        ac_phone:true,
        ac_code:false
      })
    } else {
      this.setData({
        ac_code: true,
        ac_phone: false
      })
    }
  },
  // 获取验证
  getCode:function(){
    var _this = this
    let countDownNum = this.data.times;
    if (!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(this.data.phone))) {
      wx.showToast({
        title: '请输入正确的手机号码',
        icon: 'none'
      })
      return false;
    } else {
      http.getdata('/sys/sms/send?mobile=' + this.data.phone).then((res) => {
        this.setData({
          codeget: false
        })
        this.setData({
          timer: setInterval(function () {
            countDownNum--;
            _this.setData({
              times: countDownNum
            })
            if (countDownNum == 0) {
              _this.setData({
                codeget: true,
                times: 60
              })
              clearInterval(_this.data.timer);
            }
          }, 1000)
        })
      })
    }
  },
  toAgreement:function(){
    wx.navigateTo({
      url: '../agreement/agreement',
    })
  }
})